/**
	File Manager Browser:

	options:
		- base_folder
		- can_create
		- can_upload
		- can_rename
		- can_delete
		- mode ( 'tree', 'folder', 'both' )

		- confirm
		- cancel
*/
// {{{ FMBrowser 
var FMBrowser = function ( name, options )
{
	this._init_options ( options );
	var self = this;

	this.name = name;
	this._dest_div = null;

	this._nodes = dict ();
	this._selection = dict ();

	this.tree = new OS3Tree.instance ( this.name + '-tree' );
	this.tree.expand_request = function ()
	{
		self._tree_expand.apply ( self, arguments );
	};
	this.tree.set_event ( 'click', function (  )
	{
		self._tree_click.apply ( self, arguments );
	} );

	if ( this.options [ 'base_folder' ] == 0 )
		this.tree.add_folder ( null, "(root)", 0, 0 );
	else
	{
		var dct = {
			action: 'file_manager.ajax.stat',
			id_node: this.options [ 'base_folder' ]
		};
		file_manager.ajax ( dct, function ( res )
		{
			console.debug ( res );
		} );
	}

	FMBrowser._instances [ this.name ] = this;
};
// }}}

FMBrowser._instances = dict ();

// {{{ FMBrowser.prototype.render ( dest_div )
FMBrowser.prototype.render = function ( dest_div )
{
	if ( ! dest_div ) dest_div = this._dest_div;
	this._dest_div = $el ( dest_div );

	var dct = {
		'name': this.name,
		'class': 'fm-mode-' + this.options [ 'mode' ]
	};
	$( dest_div, String.formatDict ( file_manager.templates [ 'fm-browser' ], dct ) );

	this.tree.render ( this.name + '-tree' );

	$( this.name + '-tree-toolbar', String.formatDict ( file_manager.templates [ 'fm-tree-toolbar' ], dct ) );
	$( this.name + '-folder-toolbar', String.formatDict ( file_manager.templates [ 'fm-folder-toolbar' ], dct ) );

	//this.tree.list [ 0 ].expand ();
	this._expand_all ( this.tree.list );
};
// }}}
// {{{ FMBrowser.prototype.dialog_show
FMBrowser.prototype.dialog_show = function ()
{
	liwe.dom.add_class ( this._get_dialog_id ( 'container' ), 'visible' );
};
// }}}
// {{{ FMBrowser.prototype.dialog_hide
FMBrowser.prototype.dialog_hide = function ()
{
	liwe.dom.del_class ( this._get_dialog_id ( 'container' ), 'visible' );
};
// }}}
// {{{ FMBrowser.prototype.folder_add
FMBrowser.prototype.folder_add = function ()
{
	var self = this;
	var name = prompt ( "Nome della nuova cartella:" );
	if ( ! name ) return;

	var dct = {
		action: 'file_manager.ajax.folder_create',
		folder_name: name,
		id_parent: this._last_folder
	};

	file_manager.ajax ( dct, function ( res )
	{
		self._tree_refresh ();
		self._load_files ( self._last_folder );
	} );
};
// }}}
// {{{ FMBrowser.prototype.file_rename
FMBrowser.prototype.file_rename = function ()
{
	var frm = this._rename_form ( this._last_file );
	this.dialog_show ();
	frm.set ( this._get_dialog_id () );
};
// }}}
// {{{ FMBrowser.prototype.folder_rename
FMBrowser.prototype.folder_rename = function ()
{
	var frm = this._rename_form ( this._last_folder );
	this.dialog_show ();
	frm.set ( this._get_dialog_id () );
};
// }}}
// {{{ FMBrowser.prototype.file_upload
FMBrowser.prototype.file_upload = function ()
{
	var frm = this._upload_form ();
	this.dialog_show ();
	frm.set ( this._get_dialog_id () );
};
// }}}
// {{{ FMBrowser.prototype.file_delete
FMBrowser.prototype.file_delete = function ()
{
	var self = this;
	var nodes = this.selection_get ();

	if ( ! nodes ) return;
	nodes = nodes.join ( '|' );

	if ( ! confirm ( 'Cancellare la selezione?' ) ) return;

	file_manager.ajax ( { action: 'file_manager.ajax.remove', id_nodes: nodes }, function ( res )
	{
		self._load_files ( self._last_folder );
	} );
};
// }}}
// {{{ FMBrowser.prototype.selection_clear
FMBrowser.prototype.selection_clear = function ()
{
	this._selection.iterate ( function ( _, id )
	{
		liwe.dom.del_class ( 'fm-file-' + id, 'selected' );
	} );
	this._selection = dict ();
};
// }}}
// {{{ FMBrowser.prototype.selection_add
FMBrowser.prototype.selection_add = function ( id, index )
{
	this._sel_last_index = index;
	this._selection [ id ] = true;
	liwe.dom.add_class ( 'fm-file-' + id, 'selected' );
};
// }}}

FMBrowser.prototype.selection_get = function ()
{
	return this._selection.keys ();
};


// {{{ FMBrowser.prototype.selection_toggle
FMBrowser.prototype.selection_toggle = function ( id, index )
{
	this._sel_last_index = index;
	this._selection [ id ] = ! this._selection [ id ];
	if ( this._selection [ id ] )
		liwe.dom.add_class ( 'fm-file-' + id, 'selected' );
	else
		liwe.dom.del_class ( 'fm-file-' + id, 'selected' );
};
// }}}
// {{{ FMBrowser.prototype.selection_fill ( id, index )
FMBrowser.prototype.selection_fill = function ( id, index )
{
	if ( ! this._sel_last_index )
	{
		this.selection_add ( id, index );
		return;
	}

	var last_index = this._sel_last_index;
	if ( index == last_index ) return;

	this.selection_clear ();

	index = parseInt ( index, 10 );
	last_index = parseInt ( last_index, 10 );

	var from = index, to = last_index;

	if ( index > last_index )
	{
		from = last_index;
		to = index;
	}

	for ( var i = from; i <= to; i ++ )
	{
		var n = this._node_from_index ( i );
		this.selection_add ( n.dataset.id, i );
	}

	this._sel_last_index = last_index;
};
// }}}

FMBrowser.prototype._expand_all = function ( nodes )
{
	for ( var i = 0; i < nodes.length; i ++ )
		nodes [ i ].expand ();
};

// {{{ FMBrowser.prototype._init_options ( options )
FMBrowser.prototype._init_options = function ( options )
{
	this.options = options || {};
	this.options [ 'base_folder' ] = this.options [ 'base_folder' ] || 0;
	this.options [ 'can_create' ] = !! this.options [ 'can_create' ];
	this.options [ 'can_upload' ] = !! this.options [ 'can_upload' ];
	this.options [ 'can_rename' ] = !! this.options [ 'can_rename' ];
	this.options [ 'can_delete' ] = !! this.options [ 'can_delete' ];
	this.options [ 'mode' ] = this.options [ 'mode' ] || 'both';
	this.options [ 'start_level' ] = this.options [ 'start_level' ] || 1;
};
// }}}
// {{{ FMBrowser.prototype._get_dialog_id ( suffix )
FMBrowser.prototype._get_dialog_id = function ( suffix )
{
	return this.name + '-dialog' + ( suffix ? '-' + suffix : '' );
};
// }}}
// {{{ FMBrowser.prototype._tree_expand ( node )
FMBrowser.prototype._tree_expand = function ( node )
{
	var self = this;
	var id_folder = node ? node.val : 0;

	file_manager.ajax ( { action: 'file_manager.ajax.ls', id_folder: id_folder, show_dirs: 1, tree: 1 }, function ( res )
	{
		var nodes = res [ 'nodes' ];
		if ( ! nodes ) return;

		nodes.iterate ( function ( n, i )
		{
			self._nodes [ n [ 'val' ] ] = n [ 'text' ];
		} );

		node.expand_done ( nodes );

		if ( node.level < self.options [ 'start_level' ] )
			self._expand_all ( node.list );
	} );
};
// }}}
// {{{ FMBrowser.prototype._tree_click ( tree, evt, div, node )
FMBrowser.prototype._tree_click = function ( tree, evt, div, node )
{
	this._load_files ( node.val );
};
// }}}
// {{{ FMBrowser.prototype._load_files ( id_folder )
FMBrowser.prototype._load_files = function ( id_folder )
{
	var self = this;
	this._last_folder = id_folder;

	file_manager.ajax ( { action: 'file_manager.ajax.ls', id_folder: id_folder, show_files: 1, show_dirs: 1 }, function ( res )
	{
		var nodes = res [ 'nodes' ];
		if ( ! nodes ) return;

		var html = new String.buffer ();
		nodes.iterate ( function ( n, i )
		{
			self._nodes [ n [ 'id_node' ] ] = n [ 'name' ];
			n [ '_fm_name' ] = self.name;
			n [ '_index' ] = i;
			html.add ( String.formatDict ( file_manager.templates [ 'fm-file' ], n ) );
		} );

		$( self.name + '-folder', html );
	} );
};
// }}}
// {{{ FMBrowser.prototype._upload_form
FMBrowser.prototype._upload_form = function ()
{
	var self = this;
	var frm = new liwe.form5.instance ( this.name + '-upload-form' );
	frm.action = 'file_manager.ajax.file_add';

	frm.add ( 'file', 'files', { label: "Files", multiple: 1 } );
	frm.add ( 'progress', 'progress1', { form: frm } );
	frm.add ( 'submit', 'submit', { value: "Carica" } );
	frm.add ( 'hidden', 'id_folder' );

	frm.addEventListener ( 'submit', function ()
	{
		frm.set_value ( 'id_folder', self._last_folder );
	} );
	frm.addEventListener ( 'complete', function ()
	{
		self.dialog_hide ();
		self._load_files ( self._last_folder );
	} );

	return frm;
};
// }}}
// {{{ FMBrowser.prototype._rename_form
FMBrowser.prototype._rename_form = function ( id_node )
{
	var self = this;
	var frm = new liwe.form5.instance ( this.name + '-rename-form' );
	frm.action = 'file_manager.ajax.rename';

	frm.add ( 'text', 'name', { label: "Nome", value: this._nodes [ id_node ] } );
	frm.add ( 'submit', 'submit', { value: "Crea" } );
	frm.add ( 'hidden', 'id_node', { value: id_node } );

	frm.addEventListener ( 'complete', function ()
	{
		self.dialog_hide ();
		self._tree_refresh ();
		self._load_files ( self._last_folder );
	} );

	return frm;
};
// }}}
// {{{ FMBrowser.prototype._tree_refresh
FMBrowser.prototype._tree_refresh = function ()
{
	// START OS3TREE REFRESH
	this.tree._curr_node_sel._nodes = 0;
	this.tree._curr_node_sel.list = new Array ();
	this.tree._curr_node_sel.expand ();
	// END
};
// }}}
// {{{ FMBrowser.prototype._node_from_index ( index )
FMBrowser.prototype._node_from_index = function ( index )
{
	return this._dest_div.querySelector ( '.fm-file[data-index="' + index + '"]' );
};
// }}}

// {{{ FMBrowser.dialog_show ( name )
FMBrowser.dialog_show = function ( name )
{
	var f = FMBrowser.get ( name );
	f.dialog_show ();
};
// }}}
// {{{ FMBrowser.dialog_hide ( name )
FMBrowser.dialog_hide = function ( name )
{
	var f = FMBrowser.get ( name );
	f.dialog_hide ();
};
// }}}
// {{{ FMBrowser.folder_add ( name )
FMBrowser.folder_add = function ( name )
{
	var f = FMBrowser.get ( name );
	f.folder_add ();
};
// }}}
// {{{ FMBrowser.folder_rename ( name )
FMBrowser.folder_rename = function ( name )
{
	var f = FMBrowser.get ( name );
	f.folder_rename ();
};
// }}}
// {{{ FMBrowser.get ( name )
FMBrowser.get = function ( name )
{
	return FMBrowser._instances.get ( name );
};
// }}}
// {{{ FMBrowser.file_rename ( name )
FMBrowser.file_rename = function ( name )
{
	var f = FMBrowser.get ( name );
	f.file_rename ();
};
// }}}
// {{{ FMBrowser.file_upload ( name )
FMBrowser.file_upload = function ( name )
{
	var f = FMBrowser.get ( name );
	f.file_upload ();
};
// }}}
// {{{ FMBrowser.file_delete ( name )
FMBrowser.file_delete = function ( name )
{
	var f = FMBrowser.get ( name );
	f.file_delete ();
};
// }}}
// {{{ FMBrowser.file_click ( name, obj, ev )
FMBrowser.file_click = function ( name, obj, ev )
{
	var b = FMBrowser.get ( name );
	if ( ! b ) return;

	b._last_file = obj.dataset.id;

	var shift = ev.shiftKey, ctrl = ev.ctrlKey;

	if ( ! shift && ! ctrl )
	{
		b.selection_clear ();
		b.selection_add ( obj.dataset.id, obj.dataset.index );
		return;
	}

	if ( ctrl )
	{
		b.selection_toggle ( obj.dataset.id, obj.dataset.index );
		return;
	}

	if ( shift )
	{
		b.selection_fill ( obj.dataset.id, obj.dataset.index );
		return;
	}
};
// }}}
// {{{ FMBrowser.file_dblclick ( name, obj, ev )
FMBrowser.file_dblclick = function ( name, obj, ev )
{
	var b = FMBrowser.get ( name );
	if ( ! b ) return;

	if ( obj.dataset.folder == '1' )
		b._load_files ( obj.dataset.id );
};
// }}}


FMBrowser.confirm = function ( name )
{
	var fm = FMBrowser.get ( name );

	fm.options [ 'confirm' ] && fm.options [ 'confirm' ] ( fm );
};

FMBrowser.cancel = function ( name )
{
	var fm = FMBrowser.get ( name );

	fm.options [ 'cancel' ] && fm.options [ 'cancel' ] ( fm );
};

FMBrowser.stat = function ( id_nodes, full_path, cback )
{
	if ( id_nodes.iterate )
		id_nodes = '|'.join ( id_nodes );

	file_manager.ajax ( { action: "file_manager.ajax.stat", id_nodes: id_nodes, full_path: full_path }, function ( v )
	{
		cback && cback ( v [ 'stat' ] );
	} );
};
